package com.game.module.skill
{
    import com.dusk.util.ResUtil;
    import com.game.data.vo.PlayerDataVO;
    import com.game.data.vo.SkillVO;
    import com.game.manager.NotificationManager;
    import com.game.view.layer.LayerBase;
    
    import flash.display.Bitmap;
    
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    
    //noinspection SpellCheckingInspection
    public class SetSkillKey extends LayerBase
    {
        
        private var player:PlayerDataVO;
        private var _newSkill:SkillContainer;
        private var _newSkillVO:SkillVO;
        private var _oldSkill1:SkillContainer;
        private var _oldSkill2:SkillContainer;
        private var _oldSkill3:SkillContainer;
        private var _oldSkill4:SkillContainer;
        private var _oldSkill5:SkillContainer;
        private var closeBtn:SimpleButton;
        
        public function SetSkillKey(playerDataVO:PlayerDataVO, skillVO:SkillVO)
        {
            super();
            player = playerDataVO;
            _newSkillVO = skillVO;
        }
        
        override protected function beforeAdded():void
        {
            var mc:MovieClip = ResUtil.getLayerView("SetSkillKey") as MovieClip;
            closeBtn = mc.getChildByName("closeBtn") as SimpleButton;
            _newSkill = new SkillContainer(415,230,0);
            _oldSkill1 = new SkillContainer(230,340,1);
            _oldSkill2 = new SkillContainer(322.5,340,2);
            _oldSkill3 = new SkillContainer(415,340,3);
            _oldSkill4 = new SkillContainer(507.5,340,4);
            _oldSkill5 = new SkillContainer(600,340,5);
            var highLight:MovieClip = ResUtil.getComponentView("HighLight") as MovieClip;
            highLight.x = _newSkill.x + 5;
            highLight.y = _newSkill.y + 5;
            mc.addChild(_newSkill);
            mc.addChild(highLight);
            mc.addChild(_oldSkill1);
            mc.addChild(_oldSkill2);
            mc.addChild(_oldSkill3);
            mc.addChild(_oldSkill4);
            mc.addChild(_oldSkill5);
            addChild(mc);
            setCloseBtn(closeBtn);
        }
        
        override protected function onAdded():void
        {
            for (var i:int = 1; i <= 5; i++)
            {
                var container:SkillContainer = this["_oldSkill" + i] as SkillContainer;
                container.index = i;
                container.setKey(gc.keyConfig.getSkillKeyByIndex(i));
                container.addEventListener(MouseEvent.CLICK, setSkillKeyClick);
                var skill:SkillVO = player.getEquippedSkillByIndex(i);
                if(skill)
                {
                    var icon:Bitmap = ResUtil.getImageBitmap("skillIcon_" + skill.skillId);
                    container.setIcon(icon);
                    container.isEmpty = false;
                }
            }
            _newSkill.setIcon(ResUtil.getImageBitmap("skillIcon_" + _newSkillVO.skillId));
        }
        
        override protected function onRemoved():void
        {
            for (var i:int = 1; i <= 5; i++)
            {
                var container:SkillContainer = this["_oldSkill" + i] as SkillContainer;
                container.removeEventListener(MouseEvent.CLICK, setSkillKeyClick);
            }
            _newSkill.removeEventListener(MouseEvent.CLICK, setSkillKeyClick);
        }
        
        private function setSkillKeyClick(evt:MouseEvent):void
        {
            var containter:SkillContainer = evt.currentTarget as SkillContainer;
            var setEquippedSkill:Function = function ():void
            {
                player.removeEquippedSkillByIndex(containter.index);
                player.removeEquippedSkillBySkillId(_newSkillVO.skillId);
                _newSkillVO.skillIndex = containter.index;
                player.equippedSkill.push(_newSkillVO);
                player.updateEquippedSkill();
                NotificationManager.alert("设置技能按键成功");
                removeFromParent();
            }
            if(!containter.isEmpty)
            {
                NotificationManager.tip("是否替换该技能？", setEquippedSkill, null);
                return;
            }
            setEquippedSkill();
        }
    }
}

import com.dusk.util.FilterUtil;
import com.dusk.util.TextUtil;
import com.game.view.component.ComponentBase;

import flash.display.Bitmap;
import flash.text.TextField;
import flash.text.TextFormatAlign;

class SkillContainer extends ComponentBase
{
    public function SkillContainer(thisX:Number, thisY:Number, index:int)
    {
        super();
        drawUI();
        x = thisX;
        y = thisY;
    }
    
    private var keyText:TextField;
    public var isEmpty:Boolean = true;
    public var index:int = -1;
    
    private function drawUI():void
    {
        graphics.beginFill(0xCCCCCC, 1);
        graphics.lineStyle(5, 0, 1);
        graphics.drawRect(0, 0, 76, 76);
        graphics.endFill();
        keyText = TextUtil.creatTextField("",TextUtil.UNIVERSAL_FONT,25,TextFormatAlign.CENTER,0,false,false);
        keyText.x = 0;
        keyText.y = 22;
        keyText.width = 76;
        keyText.height = 33;
        addChild(keyText);
        FilterUtil.setGlow(this,0xFFFF66,1,30,30,1,3,true,false);
        FilterUtil.clearCache();
    }
    
    public function setIcon(img:Bitmap):void
    {
        img.x = img.y = 5;
        addChild(img);
    }
    
    public function setKey(key:String):void
    {
        keyText.text = key;
    }
    
    override protected function onRemoved():void
    {
        FilterUtil.deleteAllFilters(this);
        FilterUtil.clearCache();
    }
}
